<template>
  <el-dialog ref="selectFlow2" title="选择大屏" width="30%" :visible.sync="params.visible" :close-on-click-modal="false">
    <compTree
      ref="tree"
      :show-search="false"
      :tree-show-checkbox="params.ismutil"
      :comp-tree-height="350"
      :tree-data="treeData"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
      :tree-default-expanded-keys="treeDefaultExpandedKeys"
      @treeNodeCheck="treeNodeCheck"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
/**
 * 入参
 *  id,                   // 传入方式 this.$set(this.params, 'id', id)
 *  ismutil 是否多选       // 传入方式 this.$set(this.params, 'ismulti', false)
 *  isNullSelect          // 传入方式
 * 出参
 *  name 流程名
 *  id 流程id
 * 出参传出方式 this.ok({ name, id })
 */
import { vueGetBigScreenList } from '@/api/bigScreen'
import { ParamArr, SQLTable } from '@/../public/js/paramarr'
import Global from '@/../public/js/global'
export default {
  components: { },
  props: {
    params: {
      type: Object,
      default() {
        return {
          id: '', // 大屏id
          visible: false,
          ismutil: false// 是否多选
        }
      }
    }
  },
  data() {
    return {
      treeData: [],
      data: {},
      checkedNodes: [], // checkbox选中的所有节点
      treeDefaultCheckedKeys: [],
      treeDefaultExpandedKeys: []
    }
  },
  created() {
    this.data = this._.cloneDeep(this.params)
    this.initWindow()
  },
  methods: {
    initWindow() {
      vueGetBigScreenList(new ParamArr()).then(vPar => {
        const nodes = []
        const nodeMap = new Map()
        let table = new SQLTable(vPar.getTable('k_bigscreen_group'))
        table.first()
        while (!table.eof()) {
          const pnode = Global.getTreeviewPNode(nodeMap, nodes, table.getValueByName('PID'))
          const node = {}
          node['id'] = table.getValueByName('ID')
          node['name'] = table.getValueByName('Name')
          node['text'] = node['name']
          node['icon'] = 'wenjianyi'
          node['type'] = 0
          node['folder'] = true
          pnode.push(node)
          nodeMap.set(node['id'], node)
          table.next()
        }
        table = new SQLTable(vPar.getTable('k_bigscreen'))
        table.first()
        while (!table.eof()) {
          const pnode = Global.getTreeviewPNode(nodeMap, nodes, table.getValueByName('PID'))
          const node = {}
          node['id'] = table.getValueByName('ID')
          node['pid'] = table.getValueByName('PID')
          node['name'] = table.getValueByName('Name')
          node['text'] = table.getValueByName('Name')
          node['url'] = table.getValueByName('Url')
          node['icon'] = 'daping'
          if (node.id === this.data.id) {
            this.treeDefaultExpandedKeys.push(node.pid)
            if (this.params.ismutil) {
              this.treeDefaultCheckedKeys.push(node.id)
            }
            this.$refs.tree.setSelect(node.id)
          }
          pnode.push(node)
          nodeMap.set(node['id'], node)
          table.next()
        }
        this.treeData = nodes
      })
    },
    treeNodeCheck(a, b) {
      this.checkedNodes = b.checkedNodes // 选中的所有节点
    },
    confirm() {
      var res = ''
      if (this.params.ismutil) {
        res = this.$refs.tree.getCheckedNodes()
      } else {
        res = this.$refs.tree.getCurrentNode()
      }
      if (this.params.confirm !== undefined) {
        this.params.confirm(res)
      } else {
        this.$emit('confirm', res)
      }
    }
  }
}
</script>

<style scoped>
</style>
